package com.selfdriving.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.bean.NearByPeople;
import com.selfdriving.utils.PhotoUtils;
import com.selfdriving.view.HandyTextView;

public class NearByPeopleAdapter extends BaseExpandableListAdapter
{

	LayoutInflater inflater;
	List<Map<String, Object>> datas;
	int[] tags;
	Context context;
	BaseApplication application;

	public NearByPeopleAdapter(ExpandableListView mExpandableListView,
			BaseApplication application, Context context,
			List<Map<String, Object>> datas)
	{
		inflater = LayoutInflater.from(context);
		this.application = application;
		this.context = context;
		this.datas = datas;
		getTages();
		setOnClick(mExpandableListView);
	}

	public void setOnClick(ExpandableListView mExpandableListView)
	{
		mExpandableListView
				.setOnGroupExpandListener(new OnGroupExpandListener()
				{

					@Override
					public void onGroupExpand(int arg0)
					{
						// TODO Auto-generated method stub
						tags[arg0] = 1;
					}
				});
		mExpandableListView
				.setOnGroupCollapseListener(new OnGroupCollapseListener()
				{

					@Override
					public void onGroupCollapse(int arg0)
					{
						// TODO Auto-generated method stub
						tags[arg0] = 0;
					}
				});
	}

	public void getTages()
	{
		tags = new int[datas.size()];
		for (int i = 0; i < datas.size(); i++)
		{
			tags[i] = 0;
		}
	}

	@Override
	public int getGroupCount()
	{
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public int getChildrenCount(int groupPosition)
	{
		// TODO Auto-generated method stub
		Map<String, Object> map = (Map) getGroup(groupPosition);
		List<Map<String, Object>> list = (List) map.get("item");
		return list.size();
	}

	@Override
	public Object getGroup(int groupPosition)
	{
		// TODO Auto-generated method stub
		return datas.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition)
	{
		// TODO Auto-generated method stub
		Map<String, Object> map = (Map) getGroup(groupPosition);
		List<Map<String, Object>> list = (List) map.get("item");
		return list.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition)
	{
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition)
	{
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds()
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent)
	{
		GroupHolder groupHolder;
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.list_group_item, null);
			groupHolder = new GroupHolder();
			groupHolder.img = (ImageView) convertView
					.findViewById(R.id.tag_img);
			groupHolder.title = (TextView) convertView
					.findViewById(R.id.title_view);
			groupHolder.itemCount = (TextView) convertView
					.findViewById(R.id.item_count);
			convertView.setTag(groupHolder);
		} else
		{
			groupHolder = (GroupHolder) convertView.getTag();
		}
		if (tags[groupPosition] == 0)
		{
			groupHolder.img.setImageResource(R.drawable.list_indecator_button);
		} else
		{
			groupHolder.img
					.setImageResource(R.drawable.list_indecator_button_down);
		}
		Map<String, Object> map = (Map) getGroup(groupPosition);
		groupHolder.title.setText(map.get("name").toString());
		groupHolder.itemCount.setText("" + getChildrenCount(groupPosition));
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.w_listitem_user, null);
			holder = new ViewHolder();

			holder.mIvAvatar = (ImageView) convertView
					.findViewById(R.id.user_item_iv_avatar);
			holder.mIvVip = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_vip);
			holder.mIvGroupRole = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_group_role);
			holder.mIvIndustry = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_industry);
			holder.mIvWeibo = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_weibo);
			holder.mIvTxWeibo = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_txweibo);
			holder.mIvRenRen = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_renren);
			holder.mIvDevice = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_device);
			holder.mIvRelation = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_relation);
			holder.mIvMultipic = (ImageView) convertView
					.findViewById(R.id.user_item_iv_icon_multipic);

			holder.mHtvName = (HandyTextView) convertView
					.findViewById(R.id.user_item_htv_name);
			holder.mHtvSign = (HandyTextView) convertView
					.findViewById(R.id.user_item_htv_sign);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		NearByPeople people = (NearByPeople) getChild(groupPosition,
				childPosition);
		holder.mIvAvatar.setImageBitmap(application.getAvatar(people
				.getAvatar()));
		holder.mHtvName.setText(people.getName());
		holder.mHtvSign.setText(people.getSign());
		if (people.getIsVip() != 0)
		{
			holder.mIvVip.setVisibility(View.VISIBLE);
		} else
		{
			holder.mIvVip.setVisibility(View.GONE);
		}
		if (people.getIsGroupRole() != 0)
		{
			holder.mIvGroupRole.setVisibility(View.VISIBLE);
			if (people.getIsGroupRole() == 1)
			{
				holder.mIvGroupRole
						.setImageResource(R.drawable.ic_userinfo_groupowner);
			}
		} else
		{
			holder.mIvIndustry.setVisibility(View.GONE);
		}
		if (!android.text.TextUtils.isEmpty(people.getIndustry()))
		{
			holder.mIvIndustry.setVisibility(View.VISIBLE);
			holder.mIvIndustry.setImageBitmap(PhotoUtils.getIndustry(context,
					people.getIndustry()));
		} else
		{
			holder.mIvIndustry.setVisibility(View.GONE);
		}
		if (people.getIsbindWeibo() != 0)
		{
			holder.mIvWeibo.setVisibility(View.VISIBLE);
			if (people.getIsbindWeibo() == 1)
			{
				holder.mIvWeibo.setImageResource(R.drawable.ic_userinfo_weibov);
			}
		} else
		{
			holder.mIvWeibo.setVisibility(View.GONE);
		}
		if (people.getIsbindTxWeibo() != 0)
		{
			holder.mIvTxWeibo.setVisibility(View.VISIBLE);
			if (people.getIsbindTxWeibo() == 1)
			{
				holder.mIvTxWeibo
						.setImageResource(R.drawable.ic_userinfo_tweibov);
			}
		} else
		{
			holder.mIvTxWeibo.setVisibility(View.GONE);
		}

		if (people.getIsbindRenRen() != 0)
		{
			holder.mIvRenRen.setVisibility(View.VISIBLE);
		} else
		{
			holder.mIvRenRen.setVisibility(View.GONE);
		}
		if (people.getDevice() != 0)
		{
			holder.mIvDevice.setVisibility(View.VISIBLE);
			if (people.getDevice() == 1)
			{
				holder.mIvDevice
						.setImageResource(R.drawable.ic_userinfo_android);
			}
			if (people.getDevice() == 2)
			{
				holder.mIvDevice.setImageResource(R.drawable.ic_userinfo_apple);
			}
		} else
		{
			holder.mIvDevice.setVisibility(View.GONE);
		}
		if (people.getIsRelation() != 0)
		{
			holder.mIvRelation.setVisibility(View.VISIBLE);
		} else
		{
			holder.mIvRelation.setVisibility(View.GONE);
		}
		if (people.getIsMultipic() != 0)
		{
			holder.mIvMultipic.setVisibility(View.VISIBLE);
		} else
		{
			holder.mIvMultipic.setVisibility(View.GONE);
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition)
	{
		// TODO Auto-generated method stub
		return true;
	}

	class GroupHolder
	{
		ImageView img;
		TextView title;
		TextView itemCount;
	}

	class ViewHolder
	{
		ImageView mIvAvatar;
		ImageView mIvVip;
		ImageView mIvGroupRole;
		ImageView mIvIndustry;
		ImageView mIvWeibo;
		ImageView mIvTxWeibo;
		ImageView mIvRenRen;
		ImageView mIvDevice;
		ImageView mIvRelation;
		ImageView mIvMultipic;
		HandyTextView mHtvName;
		HandyTextView mHtvSign;
	}
}
